import React from 'react';
import {Card, Descriptions, Drawer, Spin} from 'antd';
import styles from '@/utils/utils.less'
import {SysAppModel} from '../data';
import useLoad from "@/hooks/useLoad";
import service from "../service";
import {DetailsDialogProps} from "@/global";
import StatusTag from "@/components/StatusTag";
import {isAdminUser} from "@/utils/utils";

/** 系统应用详情组件 */
const SysAppDetails: React.FC<DetailsDialogProps> = props => {
  const {params: {id}, visible, hide} = props;
  const {loading, data: model} = useLoad<SysAppModel>({
    isLoad: Boolean(id),
    onLoad: () => service.selectById(id),
  }, [id]);

  return (
    <Drawer
      title="查看系统应用详细信息"
      width="50%"
      visible={visible}
      onClose={hide}
      destroyOnClose
      className={styles.drawerBodyPadding}
    >
      <Spin spinning={loading} wrapperClassName={styles.h100p}>
        {model && (<Basic model={model}/>)}
      </Spin>
    </Drawer>
  );
};

function Basic(props: { model: SysAppModel }) {
  const {model} = props;
  return (
    <Card bordered={false} className={styles.cardBodyDesc}>
      <Descriptions column={1} bordered={true} size="small" className={styles.detailsDescriptions}>
        {isAdminUser() && (<Descriptions.Item label="应用主键">{model.id}</Descriptions.Item>)}
        <Descriptions.Item label="应用名称">{model.name}</Descriptions.Item>
        <Descriptions.Item label="状态">
          <StatusTag status={model.status} trueLable="启用" falseLable="禁用"/>
        </Descriptions.Item>
        <Descriptions.Item label="备注">{model.remark}</Descriptions.Item>
      </Descriptions>
    </Card>
  );
}

export default React.memo(SysAppDetails);
